package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.tencent.qcloud.ugckit.utils.ToastUtil;
import com.whmnx.base.bean.ImgResultBean;
import com.whmnx.base.bean.OrderDetailBean;
import com.whmnx.base.bean.RequestBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.FileUtil;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.databinding.ActivityRequestBinding;
import com.whmnx.my.ui.activity.RequestActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/10/28 0028 15:14
 * version: 1.0
 * @author Administrator
 */
public class RequestModel extends ToolbarViewModel{

    public ObservableField<String> Product_Name = new ObservableField<>("");
    public ObservableField<String> Product_Price = new ObservableField<>("");
    public ObservableField<String> Product_Num = new ObservableField<>("");
    public ObservableField<String> Product_SPEC = new ObservableField<>("");
    public ObservableField<String> Price = new ObservableField<>("");
    public ObservableField<String> etInput = new ObservableField<>("");

    public  RequestActivity mActivity;
    public  ActivityRequestBinding mBinding;
    public  String orderId;

    private String photosUrl = "";

    private List<String> data;

    private  ArrayList<String> list_path = new ArrayList<>();
    private String order_orderNo;

    public RequestModel(@NonNull Application application) {
        super(application);
    }

    public void initToolbar(RequestActivity requestActivity, ActivityRequestBinding binding, String orderId) {
        this.mActivity = requestActivity;
        this.orderId = orderId;
        this.mBinding = binding;
        setTitleText("申请退货");
        getOrderdetail();
        order_orderNo = mActivity.getIntent().getStringExtra("Order_OrderNo");
    }

    private void getOrderdetail() {
        Map<String, Object> params = new HashMap<>();
        params.put("OrderId",orderId);
        HttpClient.get(mActivity, Constants.ORDERDETAIL, params,new CallBack<OrderDetailBean>() {
            @Override
            public void onSuccess(OrderDetailBean result) {
                Price.set("￥" + result.getTotalAmount());
                observableList.clear();
                for (OrderDetailBean.ProductListBean productListBean : result.getProductList()) {
                    observableList.add(new OrderItemViewModel4(RequestModel.this,productListBean));
                }
            }
        });
    }

    //给RecyclerView添加ObservableList
    public ObservableList<OrderItemViewModel4> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<OrderItemViewModel4> itemBinding = ItemBinding.of(BR.viewModel, R.layout.grid_order4);



    //提交
    public BindingCommand OnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            data = mBinding.snplMomentAddPhotos.getData();
            if (TextUtils.isEmpty(etInput.get())){
                ToastUtil.toastShortMessage("请输入退货说明");
                return;
            }

            if (data.size()==0){
                ToastUtil.toastShortMessage("请先添加图片");
                return;
            }
            list_path.clear();
            List<File> files = new ArrayList<>();
            for (String datum : data) {
                File file = new File(FileUtil.compressImage(datum));
                files.add(file);
            }

            HttpClient.postImg(mActivity, Constants.UPLOADHEADIMG, files, new CallBack<List<ImgResultBean>>() {
                @Override
                public void onSuccess(List<ImgResultBean> baseBean) {
                    for (ImgResultBean imgResultBean : baseBean) {
                        list_path.add(imgResultBean.shortPath);
                    }
                    getUpdate();
                }
            });
        }
    });

    private void getUpdate() {
        RequestBean requestBean  =  new RequestBean();
        requestBean.setOrder_OrderNo(order_orderNo);
        requestBean.setBackRemark(etInput.get());
        requestBean.setOrder_State(11);
        RequestBean.ImageModelBean imageModelBean =  new RequestBean.ImageModelBean();

        for (String s : list_path) {
            String s1;
            if (s.contains("http")){
                s1 = s.split(Constants.HOST)[1];
            }else {
                s1 = s;
            }
            photosUrl += s1 + ",";
        }
        imageModelBean.setImage(photosUrl);

        List<RequestBean.ImageModelBean> imageModel = new ArrayList<>();
        imageModel.add(imageModelBean);

        requestBean.setImageModel(imageModel);

        Map<String, Object> params = new HashMap<>();
        params.put("",requestBean);
        HttpClient.post(mActivity, Constants.BACKORDER, params,new CallBack<OrderDetailBean>() {
            @Override
            public void onSuccess(OrderDetailBean result) {
                RxBus.getDefault().post(EventConstants.REQUEST);
                finish();
            }
        });
    }
}
